package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisManageOperateLogRpcService;
import com.hunttown.mes.rpc.domain.AnalysisManageOperateLogDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageOperateLogDTOQuery;
import com.hunttown.mes.service.AnalysisManageOperateLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisManageOperateLogConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:05:35
 */
@Service
public class AnalysisManageOperateLogRpcServiceImpl implements AnalysisManageOperateLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageOperateLogRpcServiceImpl.class);

    private final AnalysisManageOperateLogService analysisManageOperateLogService;

    @Autowired
    public AnalysisManageOperateLogRpcServiceImpl(AnalysisManageOperateLogService analysisManageOperateLogService) {
        this.analysisManageOperateLogService = analysisManageOperateLogService;
    }

    @Override
    public AnalysisManageOperateLogDTO insert(AnalysisManageOperateLogDTO analysisManageOperateLogDTO) {
        try {
            return convert(analysisManageOperateLogService.insert(convert(analysisManageOperateLogDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManageOperateLog create failed.", e);
            throw new RuntimeException("AnalysisManageOperateLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageOperateLogDTO analysisManageOperateLogDTO) {
        try {
            return analysisManageOperateLogService.updateInfoById(convert(analysisManageOperateLogDTO));
        } catch (Exception e) {
            logger.error("AnalysisManageOperateLog update by id failed.", e);
            throw new RuntimeException("AnalysisManageOperateLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageOperateLogDTOQuery analysisManageOperateLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageOperateLogDTOQuery.getW_id() != null && analysisManageOperateLogDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageOperateLogDTOQuery.getW_id());
            }

            if (analysisManageOperateLogDTOQuery.getIds() != null && !analysisManageOperateLogDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageOperateLogDTOQuery.getIds());
            }

            return analysisManageOperateLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManageOperateLog update by query failed.", e);
            throw new RuntimeException("AnalysisManageOperateLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageOperateLogDTO selectById(Integer id) {
        try {
            return convert(analysisManageOperateLogService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManageOperateLog select by id failed.", e);
            throw new RuntimeException("AnalysisManageOperateLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageOperateLogDTO selectObjByQuery(AnalysisManageOperateLogDTOQuery analysisManageOperateLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisManageOperateLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManageOperateLog select obj by query failed.", e);
            throw new RuntimeException("AnalysisManageOperateLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageOperateLogDTO> getListForPage(AnalysisManageOperateLogDTOQuery analysisManageOperateLogDTOQuery) {
        Query query = Query.page(analysisManageOperateLogDTOQuery.getPn(), analysisManageOperateLogDTOQuery.getPageSize());

        if (analysisManageOperateLogDTOQuery.getIds() != null && !analysisManageOperateLogDTOQuery.getIds().equals("")) {
            query.put("ids", analysisManageOperateLogDTOQuery.getIds());
        }

        if (analysisManageOperateLogDTOQuery.getLikeName() != null && !analysisManageOperateLogDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisManageOperateLogDTOQuery.getLikeName());
        }

        if (analysisManageOperateLogDTOQuery.getOrderBy() != null && analysisManageOperateLogDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisManageOperateLogDTOQuery.getOrderBy());
        }

        List<AnalysisManageOperateLogDTO> list = convert(analysisManageOperateLogService.selectByQuery(query));
        Page<AnalysisManageOperateLogDTO> page = Page.from(analysisManageOperateLogDTOQuery.getPn(), analysisManageOperateLogDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageOperateLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageOperateLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManageOperateLog delete failed.", e);
            throw new RuntimeException("AnalysisManageOperateLog delete failed, Please order your code.", e);
        }
    }
}